package cn.crc.sysinfo;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.IOException;

/**
 * 解析读写xml文件_23.9.4-lhz
 */
public class XmlUtil {
    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException {
        XmlUtil stu = new XmlUtil();
        stu.getDocument();
        //stu.getShowInfo();
        //stu.addInfo();
        //stu.updateInfo();
        //stu.deleteInfo();
    }

    private String filepath="F://student.xml";
    private Document document=null;
    private void deleteInfo() {
        NodeList stuList = document.getElementsByTagName("stu");
        for(int i = 0;i<stuList.getLength();i++) {
            Element item = (Element) stuList.item(i);
            if (item.getNodeType() == Node.ELEMENT_NODE) {
                NodeList itemer = item.getChildNodes();
                for (int j = 0; j < itemer.getLength(); j++) {
                    Node node = itemer.item(j);
                    if(node.getNodeType()==Node.ELEMENT_NODE){
                        Element element = (Element) node;
                        if(element.getAttribute("name").equals("张三")){
                            item.getParentNode().removeChild(item);
                        }
                    }
                }
            }
        }
        save();
    }
    private void updateInfo() {
        NodeList gradeList = document.getElementsByTagName("gradeName");
        for(int i=0;i<gradeList.getLength();i++){
            Node item = gradeList.item(i);
            Element ele = (Element)item;
            ele.setAttribute("id",i+"");
        }
        NodeList stuList  = document.getElementsByTagName("stu");
        for(int i=0;i<stuList.getLength();i++){
            Node item = stuList.item(i);
            Element ele = (Element)item;
            ele.setAttribute("id",i+"");
        }
        save();
    }

    private void addInfo() {
        Element stu = document.createElement("stu");
        stu.setAttribute("name","第三个学生");
        Element type1 = document.createElement("stuNo");
        type1.setAttribute("name","1003");
        Element type2 = document.createElement("stuName");
        type2.setAttribute("name","王五");
        stu.appendChild(type1);
        stu.appendChild(type2);
        Element element= document.getDocumentElement();
        NodeList childNodes = element.getChildNodes();
        for(int i=0;i<childNodes.getLength();i++){
            Node item = childNodes.item(i);
            if(item.getNodeType()==Node.ELEMENT_NODE){
                Element ele = (Element) item;
                if(ele.getAttribute("name").equals("学生")){
                    ele.appendChild(stu);
                }
            }
        }
        save();
    }

    private void save() {
        TransformerFactory factory = TransformerFactory.newInstance();
        try {
            Transformer transformer = factory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT,"yes");
            DOMSource source = new DOMSource(document);
            StreamResult result = new StreamResult(new File(filepath));
            transformer.transform(source,result);
        } catch (TransformerException e) {
            throw new RuntimeException(e);
        }
    }

    private void getShowInfo() {
        Element root = document.getDocumentElement();
        NodeList childNodes = root.getChildNodes();
        showChildNodes(childNodes);
    }

    private void showChildNodes(NodeList childNodes) {
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node item = childNodes.item(i);
            if(item.getNodeType() == Node.ELEMENT_NODE){
                Element ele = (Element) item;
                System.out.println(ele.getTagName()+"\t"+ele.getAttribute("name"));
                showChildNodes(ele.getChildNodes());
            }
        }
    }

    private void getDocument() throws ParserConfigurationException, IOException, SAXException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        document = builder.parse(filepath);
    }

}
/*
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<list>
<!--所有年级-->
<classes name="班级">
<grade name="java">
<gradeName name="一年级"/>
<gradeName name="二年级"/>
</grade>
<grade name="html">
<gradeName name="三年级"/>
<gradeName name="四年级"/>
</grade>
</classes>
<!--所有学生-->
<stuList name="学生">
<stu name="第一个学生">
<stuNo name="1001"/>
<stuName name="张三"/>
</stu>
<stu name="第二个学生">
<stuNo name="1002"/>
<stuName name="李四"/>
</stu>
</stuList>
</list>
*/
